var mUrl = "/m"
//TaskShare
function shareTask(taskId, userId) {
    $("#shareTaskId").val(taskId);
    var shareUserList = $("#shareUserList");
    shareUserList.html("<div>加载中...</div>");

    // 先加载分享过的用户
    loadShareUsers(taskId, function () {
        jQuery.ajax({
            type: "GET",
            url: mUrl + "/userInfo/simple/list/json",
            cache: false,
            success: function (ret) {
                var userInfoList = JSON.parse(ret);
                var content = [];
                var hasSelectAll = true;
                for (var i = 0; i < userInfoList.length; i++) {
                    var userInfo = userInfoList[i];
                    if (userInfo.id == userId || userInfo.userName == null) {
                        continue;
                    }

                    // 当前用户是否被分享
                    var has = hasShareUser(userInfo.id);
                    var checked = "";
                    if(has){
                        checked = "checked='checked'";
                    } else {
                        hasSelectAll = false;
                    }

                    content.push("<div style='padding-top:1px;'><label>"
                        +"<input type='checkbox' name='shareUser' value='" + userInfo.id + "' "+checked+">"
                        + userInfo.userName + "</label></div>")
                }

                // 设置 全选
                $("#chkSelectAllShareUser").prop("checked",hasSelectAll);

                var html = content.join("");
                shareUserList.html(html);
            }
        });
    })

    // 显示
    showShare();
}

var shareUsers = null;

function loadShareUsers(shareTaskId, func) {
    shareUsers = null;
    jQuery.ajax({
        type: "GET",
        url: mUrl + "/task/share/user/list/json",
        data: {"taskId": shareTaskId},
        cache: false,
        success: function (ret) {
            if(ret != "") {
                shareUsers = JSON.parse(ret);
            }

            if (func) {
                func();
            }
        }
    });
}

function hasShareUser(userId) {
    if (shareUsers == null) {
        return false;
    }

    for (var i = 0; i < shareUsers.length; i++) {
        var shareUser = shareUsers[i];
        if(shareUser.id == userId){
            return true;
        }
    }

    return false;
}

// 分享弹窗居中
$('#myShareModal').on('show.bs.modal', function (e) {
    $(this).find('.modal-dialog').css({
        'margin-top': function () {
            var modalHeight = 400; // $('#myModal').find('.modal-dialog').height();
            return ($(window).height() / 2 - (modalHeight / 2));
        }
    });
});

function showShare() {
    $("#myShareModal").modal("show");
}

function hideShare() {
    $("#myShareModal").modal("hide");
}

function doShareTask() {
    var shareTaskId = $("#shareTaskId").val();

    var userIdArr = [];
    $("input[name='shareUser']").each(function () {
        var $this = $(this);
        if ($this.is(":checked")) {
            var userId = $this.val();
            userIdArr.push(userId);
        }
    });

    var userIds = userIdArr.join(",");

    jQuery.ajax({
        type: "POST",
        url: mUrl + "/task/share",
        data: {"taskId": shareTaskId, "shareUserIds": userIds},
        success: function (ret) {
            if (ret > 0 || userIds == "") {
                alert("分享成功");
                hideShare();
            } else {
                alert("分享失败");
            }
        }
    });
}

function selectAllShareUser(col){
    var $col = $(col);
    var isChecked = $col.is(":checked");
    $("input[name='shareUser']").prop("checked",isChecked);
}